-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local skynet = require 'skynet'
require 'skynet.manager'
local logging = require 'mc.logging'
local spdm_app = require 'spdm_app'
local skynet_config = require 'skynet_config'

local app = nil
local command = {}

function command.exit()
    skynet.timeout(0, function()
        logging:info('- spdm service exit')
        skynet.exit()
    end)
end

skynet.start(function()
    local ok
    logging:notice('spdm app uniqueservice')
    skynet.uniqueservice('sd_bus')
    logging:notice('spdm app register service')
    skynet.register(skynet_config.SERVICE_NAME)
    logging:notice('new spdm app start')
    ok, app = pcall(spdm_app.new)
    if not ok or not app then
        logging:error('new spdm app failed, error info : %s', app)
    end
    logging:notice('new spdm app success')
    skynet.dispatch('lua', function(_, _, cmd, ...)
        local f = assert(command[cmd])
        skynet.ret(skynet.pack(f(...)))
    end)
    logging:notice('spdm app dispatch')
end)
